package com.chen.config;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;
import java.util.HashMap;

public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String data = authException.getLocalizedMessage();
        HashMap<Object, Object> result = new HashMap<>();
        response.setContentType("application/json;charset=UTF-8");
        result.put("code", 401);
        result.put("message", "用户未登录");
        result.put("data", data);
        response.getWriter().print(JSON.toJSONString(result));
    }
}
